package com.example.service;

import cn.hutool.core.date.DateUtil;
import com.example.common.enums.LeaveEnum;
import com.example.common.enums.LevelEnum;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.*;
import com.example.exception.CustomException;
import com.example.mapper.EmployeeMapper;
import com.example.mapper.ResourceapplyMapper;
import com.example.mapper.ResourcesMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 资产申请审批信息表业务处理
 **/
@Service
public class ResourceapplyService {

    @Resource
    private ResourceapplyMapper resourceapplyMapper;
    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private ResourcesMapper resourcesMapper;

    /**
     * 新增
     */
    public void add(Resourceapply resourceapply) {
        // 获取一下前台传过来的资产数量
        Integer num = resourceapply.getNum();
        // 根据前台表单传过来的资产id，去资产表查数量，判断数据库中够不够所需要的数量
        Resources resources = resourcesMapper.selectById(resourceapply.getResourcesId());
        // 如果数量不够，给一个提示
        if (resources.getNum() < num) {
            throw new CustomException(ResultCodeEnum.RESOURCES_NUM_ERROR);
        }
        // 根据前台表单传过来的登录中的用户id，查到完整的用户信息
        Employee employee = employeeMapper.selectById(resourceapply.getEmployeeId());
        // 根据员工信息，初始化部门id
        resourceapply.setDepartmentId(employee.getDepartmentId());
        resourceapply.setTime(DateUtil.now());

        // 判断当前用户的等级level，如果是员工，设置审批进度为"待主管审批"
        if (LevelEnum.EMPLOYEE.level.equals(employee.getLevel())) {
            resourceapply.setProcess(LeaveEnum.HEADER_APPLYING.process);
            // 如果是主管，设置审批进度为"待管理员审批"
        } else if (LevelEnum.HEADER.level.equals(employee.getLevel())) {
            resourceapply.setProcess(LeaveEnum.ADMIN_APPLYING.process);
        }
        resourceapply.setStatus(LeaveEnum.STATUS_ING.process);
        resourceapplyMapper.insert(resourceapply);

        // 该资产数量减少对应的申请数量
        resources.setNum(resources.getNum() - num);
        resourcesMapper.updateById(resources);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        resourceapplyMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            resourceapplyMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Resourceapply resourceapply) {
        String process = resourceapply.getProcess();
        // 先从JWT中获取当前用户
        Account currentUser = TokenUtils.getCurrentUser();
        // 根据前台传过来的资产申请审批的资产id，查出资产的详细信息
        Resources resources = resourcesMapper.selectById(resourceapply.getResourcesId());
        if (RoleEnum.USER.name().equals(currentUser.getRole())) {
            // 根据当前用户的id，去employee查到完整的用户信息
            Employee employee = employeeMapper.selectById(currentUser.getId());
            // 如果是主管
            if (LevelEnum.HEADER.level.equals(employee.getLevel())) {
                // 审批通过，要走到管理员那边
                if (LeaveEnum.APPLY_OK.process.equals(process)) {
                    resourceapply.setProcess(LeaveEnum.ADMIN_APPLYING.process);
                    // 审批不通过，直接结束
                } else if (LeaveEnum.APPLY_NO.process.equals(process)) {
                    // 返回资产库存
                    resources.setNum(resources.getNum() + resourceapply.getNum());
                    resourceapply.setProcess(LeaveEnum.HEADER_APPLY_NO.process);
                    resourceapply.setStatus(LeaveEnum.STATUS_NO.process);
                }
            }
            // 管理员
        } else {
            if (LeaveEnum.APPLY_OK.process.equals(process)) {
                resourceapply.setProcess(LeaveEnum.APPLY_PASS.process);
                resourceapply.setStatus(LeaveEnum.STATUS_OK.process);
            } else if (LeaveEnum.APPLY_NO.process.equals(process)) {
                // 返回资产库存
                resources.setNum(resources.getNum() + resourceapply.getNum());
                resourceapply.setProcess(LeaveEnum.ADMIN_APPLY_NO.process);
                resourceapply.setStatus(LeaveEnum.STATUS_NO.process);
            }
        }
        resourceapplyMapper.updateById(resourceapply);
    }

    /**
     * 根据ID查询
     */
    public Resourceapply selectById(Integer id) {
        return resourceapplyMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Resourceapply> selectAll(Resourceapply resourceapply) {
        return resourceapplyMapper.selectAll(resourceapply);
    }

    /**
     * 员工资产分页查询
     */
    public PageInfo<Resourceapply> selectPage(Resourceapply resourceapply, Integer pageNum, Integer pageSize) {
        Account currentUser = TokenUtils.getCurrentUser();
        // 用户(员工、主管)查看员工资产，只能看到自己的资产申请信息
        if (RoleEnum.USER.name().equals(currentUser.getRole())) {
            resourceapply.setEmployeeId(currentUser.getId());
            // 管理员查看员工资产，只查看"审批已通过"的有效记录，便于追回资产
        } else if (RoleEnum.ADMIN.name().equals(currentUser.getRole())) {
            resourceapply.setProcess(LeaveEnum.APPLY_PASS.process);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Resourceapply> list = resourceapplyMapper.selectAll(resourceapply);
        return PageInfo.of(list);
    }

    /**
     * 资产申请审批分页查询
     */
    public PageInfo<Resourceapply> selectPage2(Resourceapply resourceapply, Integer pageNum, Integer pageSize) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (RoleEnum.USER.name().equals(currentUser.getRole())) {
            Employee employee = employeeMapper.selectById(currentUser.getId());
            // 如果是主管，只查看自己部门的
            if (LevelEnum.HEADER.level.equals(employee.getLevel())) {
                resourceapply.setDepartmentId(employee.getDepartmentId());
                resourceapply.setProcess(LeaveEnum.HEADER_APPLYING.process);
            } else {
                // 如果是普通员工，只查看自己的
                resourceapply.setEmployeeId(employee.getId());
            }
            // 管理员，只查看审批进度是"待管理员审批"的
        } else {
            resourceapply.setProcess(LeaveEnum.ADMIN_APPLYING.process);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Resourceapply> list = resourceapplyMapper.selectAll(resourceapply);
        return PageInfo.of(list);
    }


}